package net.mcg_server.pass_gen;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.crs.toolkit.layout.SWTGridData;
import com.crs.toolkit.layout.SWTGridLayout;

@SuppressWarnings("serial")
public class pass_gen_ui extends JFrame{
	
	private JTextField outFld, inFld;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//System.out.println(System.getProperty("java.version"));
		
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
				
				new pass_gen_ui().setVisible(true);
			
			}
		});
	
	} // END MAIN
	
	public pass_gen_ui(){
		
		super("PassGen");
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(270,100);
		setLocation(300,200);
		setResizable(false);
		
		JPanel body = new JPanel(new SWTGridLayout(2, false));
		getContentPane().add(body);
		
		body.add(new JLabel("Input Pass Length"));
		
		inFld = new JTextField();
		
		SWTGridData data = new SWTGridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWTGridData.FILL;
		body.add(inFld, data);
		
		JButton genButton = new JButton("Generate");
		
		genButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				password pass = new password();
				
				int passLength = 8;
				
				try{
					passLength = Integer.parseInt(inFld.getText());
					if (passLength < 8 || passLength > 15){
						JOptionPane.showMessageDialog(null, 
								"Incorrect password length given. "
								+ "Please enter a whole number between 8 and 15");
					} else {
						pass.setLength(passLength);
						pass.generate();
						outFld.setText(pass.getPassword());
					}
				}catch (Exception error){
					JOptionPane.showMessageDialog(null,
							"String given. "
							+ "Please enter a whole number between 8 and 15.");
				}
			}
		});
		
		body.add(genButton);
		
		outFld = new JTextField();
		outFld.setEditable(false);
		
		data = new SWTGridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = SWTGridData.FILL;
		body.add(outFld, data);
	}

}
